package com.sky.mapper;

import com.github.pagehelper.Page;
import com.sky.anno.ToEasyService;
import com.sky.dto.OrdersPageQueryDTO;
import com.sky.dto.OrdersPaymentDTO;
import com.sky.entity.OrderDetail;
import com.sky.entity.Orders;
import com.sky.vo.HistoryOrderVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.time.LocalDateTime;
import java.util.List;

@Mapper
@Repository
public interface OrderMapper {

    Orders details(Integer id);


    Page<HistoryOrderVO> historyOrders(OrdersPageQueryDTO ordersPageQueryDTO);

    /*
    * 根据订单id查询订单详情
    * */
    @Select("select * from order_detail where order_id = #{orderId}")
    List<OrderDetail> historyOrdersDetail(Long orderId);


    /*
    * 查询历史订单详情
    * */
    @Select("select * from order_detail  where order_id = #{id} ")
    List<OrderDetail> orderDetail(Long id);

    /*
    * 根据id查询订单详情
    * */
    @Select("select * from orders where id = #{id}")
    HistoryOrderVO order(Long id);

    /*
    * 插入订单
    * */
    @Options(useGeneratedKeys = true,keyProperty = "id")
    @Insert("insert into orders(number, status, user_id, address_book_id, order_time, checkout_time, pay_method, pay_status, " +
            "amount, remark, phone, address, user_name, consignee, cancel_reason, rejection_reason, " +
            "cancel_time, estimated_delivery_time, delivery_time, pack_amount, tableware_number, tableware_status) " +
            "values (#{number},#{status},#{userId},#{addressBookId},#{orderTime},#{checkoutTime},#{payMethod},#{payStatus}," +
            "#{amount},#{remark},#{phone},#{address},#{userName},#{consignee},#{cancelReason},#{rejectionReason}," +
            "#{cancelTime},#{estimatedDeliveryTime},#{deliveryTime},#{packAmount},#{tablewareNumber},#{tablewareStatus})")
    void insert(Orders orders);

    /*
    * 批量插入订单详情
    * */
    void insertBatch(List<OrderDetail> orderDetailsList);




    /*
    * 取消订单，在数据库中更新订单状态
    * */
    @Update("update orders set status=#{status},pay_status=#{payStatus},cancel_time=#{cancelTime}  where id = #{id}")
    void cancel(Orders orders);



    /*
    * 更新支付状态信息
    * */
    @Update("update orders set pay_method=#{payMethod},status=#{status},pay_status=#{payStatus},checkout_time=#{checkoutTime} " +
            "where number=#{number} and user_id=#{userId}")
    int payment(Orders orders);


    /*
    * 处理超时订单
    * */
    @Update("update orders set status=6,rejection_reason='订单超时未支付',cancel_time=now() where status=1 and pay_status=0 and order_time<#{time}   ")
    int updateOverTimeOrders(@Param("time") LocalDateTime time);

    /*
    * 处理已经收到货但是管理端未点击已完成的订单
    * */
    @Update("update orders set status=5 where status=4")
    int updateDeliveringOrders();
}


